package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.CoachEnterDto;
import com.woniu.model.dto.VenueLoadCoachDto;
import com.woniu.model.form.*;
import com.woniu.model.model.YCoachEnter;
import com.woniu.model.param.PageCoachEnterParam;
import com.woniu.service.YCoachEnterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author EdisonLiu
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yCoachEnter")
@Api(tags = "教练入驻接口")
public class YCoachEnterController {

    @Resource
    private YCoachEnterService coachEnterService;

    @Resource
    private RedisUtil redisUtil;
    /**
     *教练入驻平台申请
     * @param form 教练入驻平台表单
     *
     * @return是否申请成功
     */
    @PostMapping("/addCoachEnterPlate")
    @ApiOperation(value = "教练入驻平台申请")
    public Result<?> addCoachEnterPlate(@RequestBody CoachEnterPlateForm form,@RequestHeader String token)throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        form.setCoachId(coachId);

        Integer i = coachEnterService.coachEnterPlate(form);
        if (!i.equals(1)){
            return Result.getResult().setMsg("申请失败失败");
        }
        return Result.getResult().setMsg("申请成功");
    }

    /**
     * 教练入驻场馆申请
     * @param form 教练入驻场馆表单
     * @return
     */
    @PostMapping("/addCoachEnterVenue")
    @ApiOperation(value = "教练入驻场馆申请")
    public Result<?> addCoachEnterVenue(@RequestBody CoachEnterPlateForm form,@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        form.setCoachId(coachId);

        Integer i = coachEnterService.addCoachEnterVenue(form);
        if (!i.equals(1)){
            return Result.getResult().setMsg("申请失败失败");
        }
        return Result.getResult().setMsg("申请成功，请勿重复提交");
    }

    /**
     *管理员通过教练的入驻平台申请
     * @param coachId 教练id
     * @return
     */
    @GetMapping("/passCoachEnterPlate")
    @ApiOperation(value = "教练入驻平台申请通过")
    public Result<?> passCoachEnterPlate( Integer coachId){
        Integer i = coachEnterService.passCoachEnterPlate(coachId);
        if (!i.equals(1)){
            return Result.getResult().setMsg("通过成功");
        }
        return Result.getResult().setMsg("入驻成功");
    }

    /**
     * 管理员驳回教练的入驻平台申请
     * @param form 教练的id和驳回原因
     * @return
     */
    @PostMapping("/rejectCoachEnterPlate")
    @ApiOperation(value = "教练入驻平台申请驳回")
    public Result<?> rejectCoachEnterPlate(@RequestBody rejectCoachEnterPlateForm form){
        Integer i = coachEnterService.rejectCoachEnterPlate(form);
        if (!i.equals(1)){
            return Result.getResult().setMsg("驳回失败");
        }
        return Result.getResult().setMsg("驳回申请成功");
    }

    /**
     * 通过教练入驻场馆申请
     * @param coachId 教练id
     * @return
     */
    @GetMapping("/passCoachEnterVenue")
    @ApiOperation(value = "教练入驻场馆申请通过")
    public Result<?> passCoachEnterVenue( Integer coachId){
        System.out.println("控制层"+ coachId);
        Integer i = coachEnterService.passCoachEnterVenue(coachId);
        if (!i.equals(1)){
            return Result.getResult().setMsg("通过成功");
        }
        return Result.getResult().setMsg("入驻成功");
    }




    /**
     * 场馆浏览教练的入驻申请 分页展示
     * @param form 分页信息，入驻申请状态
     * @return
     */
    @GetMapping("/pageCoachEnterByVenueId")
    @ApiOperation(value = "场馆浏览教练的入驻申请")
    public Result<Page<CoachEnterDto>> pageCoachEnterByVenueId(PageCoachEnterForm form, @RequestHeader String token){

        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        PageCoachEnterParam param = BeanUtil.toBean(form, PageCoachEnterParam.class);
        param.setVenueId(venueId);

        Page<CoachEnterDto> page = coachEnterService.pageCoachEnterByVenueId(param);

        return Result.newPageInstance(CoachEnterDto.class).setData(page).setMsg("查询成功").setCode(200);
    }

    /**
     * 管理员浏览教练的入驻平台申请 分页展示
     * @param form 分页信息，入驻申请状态
     * @return
     */
    @GetMapping("/pageCoachEnterPlateByVenueId")
    @ApiOperation(value = "管理员浏览教练的入驻平台申请")
    public Result<Page<CoachEnterDto>> pageCoachEnterPlateByVenueId(PageCoachEnterForm form){

        Page<CoachEnterDto> page = coachEnterService.pageCoachEnterPlateByVenueId(form);
        return Result.newPageInstance(CoachEnterDto.class).setMsg("查询成功").setCode(200).setData(page);
    }





}

